﻿using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Media;
using Nop.Core.Domain.XJ;
using Nop.Services.Configuration;
using Nop.Services.Customers;
using Nop.Services.Directory;
using Nop.Services.Media;
using Nop.Services.Security;
using Nop.Services.XJ;
using Nop.Web.Framework.Security;
using Nop.Web.Models.Home;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;

namespace Nop.Web.Controllers
{
	public partial class HomeController : BasePublicController
	{
		#region Fields
		private readonly IPermissionService _permissionService;
		private readonly ICustomerService _customerService;
		private readonly IDepartmentService _departmentService;
		private readonly ISettingService _settingService;
		private readonly IPictureService _pictureService;
		private readonly MediaSettings _mediaSettings;
		private readonly IXJInspectionService _xJInspectionService;
		private readonly IXJInspectionPlanService _xJInspectionPlanService;
		private readonly IXJMaintenanceService _xJMaintenanceService;
		private readonly IXJEquipmentStockOutService _xJEquipmentStockOutService;
		private readonly IXJEquipmentStockInService _xJEquipmentStockInService;
		#endregion

		#region Constructors
		public HomeController(
			IPermissionService permissionService,
			ICustomerService customerService,
			IDepartmentService departmentService,
			ISettingService settingService,
			IPictureService pictureService,
			MediaSettings mediaSettings,
			IXJInspectionService xJInspectionService,
			IXJInspectionPlanService xJInspectionPlanService,
			IXJMaintenanceService xJMaintenanceService,
			IXJEquipmentStockOutService xJEquipmentStockOutService,
			IXJEquipmentStockInService xJEquipmentStockInService
			)
		{
			this._permissionService = permissionService;
			this._customerService = customerService;
			this._departmentService = departmentService;
			this._settingService = settingService;
			this._pictureService = pictureService;
			this._mediaSettings = mediaSettings;
			this._xJInspectionService = xJInspectionService;
			this._xJInspectionPlanService = xJInspectionPlanService;
			this._xJMaintenanceService = xJMaintenanceService;
			this._xJEquipmentStockOutService = xJEquipmentStockOutService;
			this._xJEquipmentStockInService = xJEquipmentStockInService;
		}
		#endregion

		public virtual ActionResult Index()
		{
			return RedirectToRoute("Login");
		}

		[HttpPost]
		[UserValidator]
		public JsonResult GetMessages(Customer customer)
		{
			var model = new List<ListItemModel>();
			if (null == customer || customer.Deleted)
				return Json(new { result = true, model });

			// 维修提醒
			var wxs = _xJMaintenanceService.GetMessages(customer, pageSize: 5);
			if (wxs.Any())
			{
				foreach (var f in wxs)
				{
					model.Add(new ListItemModel
					{
						Id = f.Id,
						Title = f.Comment,
						Label = string.Format("{0} {1}", f.CustomerName, f.CreatedOn.ToString("yyyy-MM-dd")),
						Value = Enum.GetName(typeof(MaintenanceStateEnum), f.State),
						Url = string.Format("/pages/maintenance/detail?id={0}", f.Id)
					});
				}
			}
			// 出库提醒
			var outs = _xJEquipmentStockOutService.GetMessages(customer, pageSize: 5);
			if (outs.Any())
			{
				foreach (var f in outs)
				{
					model.Add(new ListItemModel
					{
						Id = f.Id,
						Title = f.SNo,
						Label = string.Format("{0} {1}", f.CustomerName, f.CreatedOn.ToString("yyyy-MM-dd")),
						Value = Enum.GetName(typeof(StockOutStateEnum), f.State),
						Url = string.Format("/pages/stockout/detail?id={0}", f.Id)
					});
				}
			}
			// 入库提醒
			var ints = _xJEquipmentStockInService.GetMessages(customer, pageSize: 5);
			if (ints.Any())
			{
				foreach (var f in ints)
				{
					model.Add(new ListItemModel
					{
						Id = f.Id,
						Title = f.SNo,
						Label = string.Format("{0} {1}", f.CustomerName, f.CreatedOn.ToString("yyyy-MM-dd")),
						Value = Enum.GetName(typeof(StockInStateEnum), f.State),
						Url = string.Format("/pages/stockin/detail?id={0}", f.Id)
					});
				}
			}

			// 其他成员：待巡检
			var dxjs = _xJInspectionPlanService.GetMyInspectionPlans(customer: customer, onlyBacklog: true, onlyActive: true, pageSize: 5);
			if (dxjs.Any())
			{
				foreach (var f in dxjs)
				{
					string title = f.Name;
					if (string.IsNullOrEmpty(title))
					{
						title = f.AimId == (int)AimTypeEnum.设备分类 ? string.Join(",", f.Categorys.Select(x => x.Name)) : string.Join(",", f.Places.Select(x => x.Name));
					}
					model.Add(new ListItemModel
					{
						Id = f.Id,
						Title = title,
						Value = Enum.GetName(typeof(InspectionTypeEnum), f.TypeId),
						Label = string.Format("{0}{1} {2}", f.TeamName, f.PersonName, f.LastUpdateOn.HasValue ? f.LastUpdateOn.Value.ToString("yyyy-MM-dd HH:mm") : ""),
						Url = string.Format("/pages/inspection/detail?id={0}", f.Id)
					});
				}
			}
			return Json(new { result = true, model });
		}

		/// <summary>
		/// 扫码返回巡检信息
		/// </summary>
		/// <param name="customerId"></param>
		/// <param name="type">码类型</param>
		/// <param name="id">码Id</param>
		/// <returns></returns>
		[HttpPost]
		public JsonResult Scan(int customerId, int type, int id)
		{
			var list = new List<ScanListItemModel>();
			var customer = _customerService.GetCustomerById(customerId);
			if (null != customer)
			{
				DateTime startOn = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
				DateTime endOn = startOn.AddMonths(1);
				switch (type)
				{
					//case (int)ScanTypeEnum.设施设备:
					//	// 设施设备巡检记录
					//	list = _xJInspectionService.GetMyInspections(customer: customer, equipmentId: id, state: (int)InspectionStateEnum.待巡检, startOn: startOn, endOn: endOn).Select(x =>
					//	{
					//		var item = new ScanListItemModel
					//		{
					//			Id = x.Id,
					//			Title = string.Format("{0} {1}", Enum.GetName(typeof(InspectionTypeEnum), x.TypeId), x.EquipmentName),
					//			Label = x.StartOn.ToString("yyyy-MM-dd"),
					//			Value = Enum.GetName(typeof(InspectionStateEnum), x.State)
					//		};
					//		return item;
					//	}).ToList();
					//	break;
					//case (int)ScanTypeEnum.打卡点:
					//	// 设施设备巡检记录
					//	list = _xJInspectionService.GetMyInspections(customer: customer, placeId: id, state: (int)InspectionStateEnum.待巡检, startOn: startOn, endOn: endOn).Select(x =>
					//	{
					//		var item = new ScanListItemModel
					//		{
					//			Id = x.Id,
					//			Title = string.Format("{0} {1}", Enum.GetName(typeof(InspectionTypeEnum), x.TypeId), x.EquipmentName),
					//			Label = x.StartOn.ToString("yyyy-MM-dd"),
					//			Value = Enum.GetName(typeof(InspectionStateEnum), x.State)
					//		};
					//		return item;
					//	}).ToList();
					//	break;
					default:
						break;
				}
			}
			return Json(new { result = true, list });
		}
	}
}
